package com.kermel.andromeda.data.settings;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kermel.andromeda.R;
import com.kermel.andromeda.data.DateDisplayType;

public class EditClockSettingsActivity extends Activity {
    private ClockSettings clockSettings;
    private CheckBox militaryTimeInput;
    private CheckBox displaySecondsInput;
    private CheckBox displayDateInput;
    private RadioGroup dateDisplayTypeGroup;
    private RadioButton dateDisplayTypeShortInput;
    private RadioButton dateDisplayTypeFullInput;
    private Button doneButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        clockSettings = SettingsManager.instance.getClockSettings();
     
        setContentView(R.layout.edit_clock_settings);
        
        storeWidgetHandles();
        
        initListeners();
        
        updateWidgetsWithValuesFromClockSettings();
    }
    
    private void storeWidgetHandles() {
        militaryTimeInput = (CheckBox)findViewById(R.id.editClockSettingsMilitaryTimeInput);
        displaySecondsInput = (CheckBox)findViewById(R.id.editClockSettingsDisplaySecondsInput);
        displayDateInput = (CheckBox)findViewById(R.id.editClockSettingsDisplayDateInput);
        dateDisplayTypeGroup = (RadioGroup)findViewById(R.id.editClockSettingsDateDisplayTypeGroup);
        dateDisplayTypeShortInput = (RadioButton)findViewById(R.id.editClockSettingsDateDisplayTypeShortInput);
        dateDisplayTypeFullInput = (RadioButton)findViewById(R.id.editClockSettingsDateDisplayTypeFullInput);
        doneButton = (Button)findViewById(R.id.editClockSettingsDoneButton);
    }
    
    private void initListeners() {
        displayDateInput.setOnCheckedChangeListener(new DisplayDateOnCheckedChangeHandler());
        doneButton.setOnClickListener(new DoneButtonAction());
    }
    
    private void updateWidgetsWithValuesFromClockSettings() {
        militaryTimeInput.setChecked(clockSettings.isMilitaryTime());
        displaySecondsInput.setChecked(clockSettings.isDisplaySeconds());
        displayDateInput.setChecked(clockSettings.isDisplayDate());
        
        updateDateDisplayTypeWithValuesFromClockSettings();
    }
    
    private void updateDateDisplayTypeWithValuesFromClockSettings() {
        DateDisplayType dateDisplayType = clockSettings.getDateDisplayType();
        
        switch (dateDisplayType) {
            case SHORT:
                dateDisplayTypeGroup.check(R.id.editClockSettingsDateDisplayTypeShortInput);
                break;
                
            case FULL:
                dateDisplayTypeGroup.check(R.id.editClockSettingsDateDisplayTypeFullInput);
                break;
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflator = getMenuInflater();
        inflator.inflate(R.menu.edit_clock_settings_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        
        if (item.getItemId() == R.id.doneMenuItem) {
            onDone();
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    private void onDone() {
        saveInputValuesIntoClockSettings();
        persistClockSettings();
        finish();
    }
    
    private void saveInputValuesIntoClockSettings() {
        clockSettings.militaryTime = militaryTimeInput.isChecked();
        clockSettings.displaySeconds = displaySecondsInput.isChecked();
        
        saveDisplayDateInputValuesIntoClockSettings();
    }
    
    private void saveDisplayDateInputValuesIntoClockSettings() {
        boolean displayDate = displayDateInput.isChecked();
        clockSettings.displayDate = displayDate;
        
        DateDisplayType dateDisplayType = DateDisplayType.NONE;
        
        if (displayDate) {
            int id = dateDisplayTypeGroup.getCheckedRadioButtonId();
            
            switch (id) {
                case R.id.editClockSettingsDateDisplayTypeShortInput:
                    dateDisplayType = DateDisplayType.SHORT;
                    break;
                    
                case R.id.editClockSettingsDateDisplayTypeFullInput:
                    dateDisplayType = DateDisplayType.FULL;
                    break;
            }
        }
        
        clockSettings.dateDisplayType = dateDisplayType;
    }
    
    private void persistClockSettings() {
        PersistClockSettings drone = new PersistClockSettings();
        drone.storeClockSettings();
    }
    
    private class DisplayDateOnCheckedChangeHandler implements OnCheckedChangeListener {
        
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            dateDisplayTypeShortInput.setEnabled(isChecked);
            dateDisplayTypeFullInput.setEnabled(isChecked);
        }
    }
    
    // TODO: Temporarily putting this here. Belongs somewhere else, but not sure where yet. Perhaps in SettingsManager, or in a new SettingsPersistenceManager?
    // I don't like the idea of ClockSettings owning this, but that may, in the long run, be the best place to put it.
    private class PersistClockSettings {
        private ClockSettingsPersister clockSettingsPersister;

        public PersistClockSettings() {
            clockSettingsPersister = new ClockSettingsPersister(getApplicationContext());
        }
        
        public void storeClockSettings() {
            clockSettingsPersister.store(clockSettings);
        }
    }
    
    private class DoneButtonAction implements OnClickListener {
        public void onClick(View v) {
            onDone();
        }
    }
}
